package com.travel.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.annotation.SysLog;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.*;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.commons.validator.groups.UpdateGroup;
import com.travel.modules.asset.entity.Goods;
import com.travel.modules.asset.entity.Route;
import com.travel.modules.asset.service.GoodsService;
import com.travel.modules.asset.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/route")
public class SysRouteController {
    @Resource
    private RouteService routeService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit= (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }

        List<Route> list = routeService.getAllRoute(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @RequestMapping("/list/current")
    public R list2Current(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit= (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }
        param.put("userId", ShiroUserGetter.getUserId());
        List<Route> list = routeService.getAllRoute(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @RequestMapping("/info")
    public R info(@RequestBody Route form) {
        String goodsId = form.getGoodsId();
        if (goodsId == null) {
            return R.error("缺少id");
        }
        Route route = routeService.getOne(new QueryWrapper<Route>().eq("goods_id", goodsId));
        return R.ok().put("page", route);
    }

    @SysLog("线路添加")
    @RequestMapping("/add")
    public R add(@RequestBody Route form) {
        form.setGoodsId("r-" + new SnowNoGenerator().generate());
        form.setUserId(form.getUserId() != null ? form.getUserId() : ShiroUserGetter.getUserId());
        form.setStatus(0);
        form.setHot(0);
        form.setReputation(0);
        form.setAdPrice(form.getAdPrice() == null ? new BigDecimal(0) : form.getAdPrice());
        form.setChPrice(form.getChPrice() == null ? new BigDecimal(0) : form.getChPrice());
        ValidatorUtils.validate(form, AddGroup.class);

        routeService.save(form);

        return R.ok().put("page", form)
                     .put("id", form.getGoodsId());
    }

    @SysLog("线路编辑")
    @RequestMapping("/edit")
    public R edit(@RequestBody Route form) {
        String goodsId = form.getGoodsId();
        Route old = routeService.getOne(new QueryWrapper<Route>().eq("goods_id", goodsId));
        if (old == null) {
            return R.error("更新目标丢失");
        }
        BeanUtils.copyProperties(form, old);
        ValidatorUtils.validate(old, UpdateGroup.class);
        routeService.updateById(old);
        if (old.getStatus() != 1) {
            goodsService.remove(new QueryWrapper<Goods>().eq("goods_id", old.getGoodsId()));
        } else {
            goodsService.save(Goods.builder().goodsId(form.getGoodsId()).tableName(Constant.T_ROUTE).build());
        }
        log.warn("用户 {} : 线路更新, 编号 : {}", ShiroUserGetter.getUserId(), goodsId);
        return R.ok();
    }

    @SysLog("线路删除")
    @RequestMapping("/del")
    public R del(@RequestBody Map<String, String> param) {
        String goodsId = param.get("goodsId");
        if (StringUtils.isBlank(goodsId)) {
            return R.error("缺少id");
        }
        Route route = routeService.getOne(new QueryWrapper<Route>().eq("goods_id", goodsId));
        if (route == null) {
            return R.error("没有该记录");
        }
        goodsService.remove(new QueryWrapper<Goods>().eq("goods_id", goodsId));
        routeService.removeById(route.getId());
        log.warn("用户: {}, 线路删除, 编号: {}", ShiroUserGetter.getUserId(), goodsId);
        return R.ok();
    }
}
